package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.QuestionTypeService;
import com.junzhiit.nengji.cms.web.exception.QuestionTypeHasUseException;
import com.junzhiit.nengji.cms.web.exception.QuestionTypeIsExistException;
import com.junzhiit.nengji.cms.web.exception.QuestionTypeNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.QuestionTypeVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.dao.QuestionDAO;
import com.junzhiit.nengji.dao.QuestionTypeDAO;
import com.junzhiit.nengji.model.QuestionType;

@Service
public class QuestionTypeServiceImpl extends BaseServiceImpl implements QuestionTypeService {
	
	@Autowired
	private QuestionTypeDAO questionTypeDAO;
	
	@Autowired
	private QuestionDAO questionDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, String name) {
		List<QuestionType> list = questionTypeDAO.findAll(dtp.getStart(), dtp.getLength(), name);
		List<QuestionTypeVO> listVO =new ArrayList<QuestionTypeVO>();
		for (QuestionType questionType : list) {
			QuestionTypeVO questionTypeVO =new QuestionTypeVO();
			BeanUtils.copyProperties(questionType, questionTypeVO);
			listVO.add(questionTypeVO);
		}
		int totalCount = questionTypeDAO.getTotalCount(name);
		return ViewObjectUtil.dataTables(listVO, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO resultVO = new ResultVO();
		QuestionType questionType = questionTypeDAO.query(id);
		if (questionType == null) {
			throw new QuestionTypeNotFoundException("该问题类型不存在！");
		}
		QuestionTypeVO questionTypeVO =new QuestionTypeVO();
		BeanUtils.copyProperties(questionType, questionTypeVO);
		resultVO.setData(questionTypeVO);
		return resultVO;
	}

	@Override
	public ResultVO add(String name) {
		ResultVO result = new ResultVO();
		
		int count = questionTypeDAO.getQuestionTypeExist(name);
		if (count > 0) {
			throw new QuestionTypeIsExistException("问题类型已经存在");
		}
		
		QuestionType questionType=new QuestionType();
		questionType.setName(name);
		QuestionType param = questionTypeDAO.insert(questionType);
		
		QuestionTypeVO questionTypeVO = new QuestionTypeVO();
		BeanUtils.copyProperties(param, questionTypeVO);
		
		result.setData(questionTypeVO);
		return result;
	}

	@Override
	public ResultVO delete(String id) {
		ResultVO result = new ResultVO();
		QuestionType param = questionTypeDAO.query(id);
		if (param == null) {
			throw new QuestionTypeNotFoundException("该问题类型不存在！");
		}
		int count=questionDAO.getTotalCount(id);
		if(count>0){
			throw new QuestionTypeHasUseException("问题类型已经被使用");
		}
		result.setData(questionTypeDAO.updateToDelete(id));
		return result;
	}

	@Override
	public ResultVO edit(String id, String name) {
		ResultVO result = new ResultVO();
		
		QuestionType param = questionTypeDAO.query(id);
		
		if (param == null) {
			throw new QuestionTypeNotFoundException("该问题类型不存在！");
		}
		if (!param.getName().equals(name)) {
			if (questionTypeDAO.getQuestionTypeExist(name) > 0) {
				throw new QuestionTypeIsExistException("问题类型已经存在");
			}
		}
		param.setName(name);
		param.setUpdatedTime(new Date());
		
		result.setData(questionTypeDAO.update(param));
		return result;
	}

	@Override
	public ResultVO getList() {
		ResultVO result = new ResultVO();
		List<QuestionType> list = questionTypeDAO.getList();
		List<QuestionTypeVO> listVO =new ArrayList<QuestionTypeVO>();
		for (QuestionType questionType : list) {
			QuestionTypeVO questionTypeVO =new QuestionTypeVO();
			BeanUtils.copyProperties(questionType, questionTypeVO);
			listVO.add(questionTypeVO);
		}
		result.setData(listVO);
		return result;
	}

	@Override
	public ResultVO toTop(String id) {
		ResultVO resultVO = new ResultVO();
		
		QuestionType questionType = questionTypeDAO.query(id);
		if (questionType == null) {
			throw new QuestionTypeNotFoundException("该问题类型不存在！");
		}
		questionType.setSort(questionTypeDAO.getMaxSort() + 1);
		questionType.setUpdatedTime(new Date());
		
		resultVO.setData(questionTypeDAO.update(questionType));
		return resultVO;
	}

}
